# -*- coding: utf-8 -*-
"""LW-test.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1jZQEZI9t5l5dKiM4OWFCyWgAMlxdVw2X
"""

from google.colab import drive
drive.mount('/content/drive')

import pandas as pd
import numpy as np
from scipy.stats import norm

##################################################################################
# Define function for Ledoit-Wolf test statistic
def ledoit_wolf_test(r1, r2, risk_free_rate):
    mean_r1 = r1.mean()
    mean_r2 = r2.mean()
    std_r1 = r1.std()
    std_r2 = r2.std()

    SR1 = (mean_r1 - risk_free_rate) / std_r1
    SR2 = (mean_r2 - risk_free_rate) / std_r2


    cov_matrix = np.cov(r1, r2)
    var_diff = (1 + 2 * (SR1 - SR2) * cov_matrix[0, 1] + (SR1 - SR2) * 2 * (cov_matrix[0, 0] / (std_r1 * 2) + cov_matrix[1, 1] / (std_r2 ** 2))) / (r1.shape[0] - 1)

    Z = (SR1 - SR2) / np.sqrt(var_diff)
    p_value = 2 * (1 - norm.cdf(np.abs(Z)))

    return Z, p_value

# Considering portfolios without Commodities
## but transaction costs included. (stocks_tc)

# Load data (Note: Replace with your local paths)
df_a2c = pd.read_csv('/content/drive/MyDrive/stocks_tc/df_daily_return_a2c.csv', index_col=0)
df_ddpg = pd.read_csv('/content/drive/MyDrive/stocks_tc/df_daily_return_ddpg.csv', index_col=0)
df_minvar = pd.read_csv('/content/drive/MyDrive/stocks_tc/df_daily_return_minvar.csv', index_col=0)
df_ppo = pd.read_csv('/content/drive/MyDrive/stocks_tc/df_daily_return_ppo.csv', index_col=0)
df_ptf = pd.read_csv('/content/drive/MyDrive/stocks_tc/df_daily_return_ptf.csv', index_col=0)
df_sac = pd.read_csv('/content/drive/MyDrive/stocks_tc/df_daily_return_sac.csv', index_col=0)
df_td3 = pd.read_csv('/content/drive/MyDrive/stocks_tc/df_daily_return_td3.csv', index_col=0)

# Fill NaN and ensure date format
df_a2c["date"] = pd.to_datetime(df_a2c["date"])
df_ddpg["date"] = pd.to_datetime(df_ddpg["date"])
df_ppo["date"] = pd.to_datetime(df_ppo["date"])
df_sac["date"] = pd.to_datetime(df_sac["date"])
df_td3["date"] = pd.to_datetime(df_td3["date"])

df_minvar = df_minvar.fillna(0)
df_ptf = df_ptf.fillna(0)

# Set risk-free rate
risk_free_rate = 0.01

# Create a list of return series and their names
return_series = [df_a2c["daily_return"], df_ddpg["daily_return"], df_minvar["account_value"],
                 df_ppo["daily_return"], df_ptf["Portfolio Returns"], df_sac["daily_return"], df_td3["daily_return"], df_td3["daily_return"]]
strategy_names = ["A2C", "DDPG", "MinVar", "PPO", "PTF", "SAC","TD3"]

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[i], return_series[j], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

# Considering portfolios without Commodities and transaction costs. (stocks_ntc)
# Load data (Note: Replace with your local paths)
df_a2c = pd.read_csv('/content/drive/MyDrive/stocks_ntc/df_daily_return_a2c.csv', index_col=0)
df_ddpg = pd.read_csv('/content/drive/MyDrive/stocks_ntc/df_daily_return_ddpg.csv', index_col=0)
df_minvar = pd.read_csv('/content/drive/MyDrive/stocks_ntc/df_daily_return_minvar.csv', index_col=0)
df_ppo = pd.read_csv('/content/drive/MyDrive/stocks_ntc/df_daily_return_ppo.csv', index_col=0)
df_ptf = pd.read_csv('/content/drive/MyDrive/stocks_ntc/df_daily_return_ptf.csv', index_col=0)
df_sac = pd.read_csv('/content/drive/MyDrive/stocks_ntc/df_daily_return_sac.csv', index_col=0)
df_td3 = pd.read_csv('/content/drive/MyDrive/stocks_ntc/df_daily_return_td3.csv', index_col=0)

# Fill NaN and ensure date format
df_a2c["date"] = pd.to_datetime(df_a2c["date"])
df_ddpg["date"] = pd.to_datetime(df_ddpg["date"])
#df_minvar["date"] = pd.to_datetime(df_minvar["date"])
df_ppo["date"] = pd.to_datetime(df_ppo["date"])
#df_ptf["date"] = pd.to_datetime(df_ptf["date"])
df_sac["date"] = pd.to_datetime(df_sac["date"])
df_td3["date"] = pd.to_datetime(df_td3["date"])

df_minvar = df_minvar.fillna(0)
df_ptf = df_ptf.fillna(0)

# Set risk-free rate
risk_free_rate = 0.01

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[i], return_series[j], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

# Considering portfolios with Commodities and transaction costs. (cmds_tc)
# Load data (Note: Replace with your local paths)
df_a2c = pd.read_csv('/content/drive/MyDrive/cmds_tc/df_daily_return_a2c.csv', index_col=0)
df_ddpg = pd.read_csv('/content/drive/MyDrive/cmds_tc/df_daily_return_ddpg.csv', index_col=0)
df_minvar = pd.read_csv('/content/drive/MyDrive/cmds_tc/df_daily_return_minvar.csv', index_col=0)
df_ppo = pd.read_csv('/content/drive/MyDrive/cmds_tc/df_daily_return_ppo.csv', index_col=0)
df_ptf = pd.read_csv('/content/drive/MyDrive/cmds_tc/df_daily_return_ptf.csv', index_col=0)
df_sac = pd.read_csv('/content/drive/MyDrive/cmds_tc/df_daily_return_sac.csv', index_col=0)
df_td3 = pd.read_csv('/content/drive/MyDrive/cmds_tc/df_daily_return_td3.csv', index_col=0)

# Fill NaN and ensure date format
df_a2c["date"] = pd.to_datetime(df_a2c["date"])
df_ddpg["date"] = pd.to_datetime(df_ddpg["date"])
#df_minvar["date"] = pd.to_datetime(df_minvar["date"])
df_ppo["date"] = pd.to_datetime(df_ppo["date"])
#df_ptf["date"] = pd.to_datetime(df_ptf["date"])
df_sac["date"] = pd.to_datetime(df_sac["date"])
df_td3["date"] = pd.to_datetime(df_td3["date"])

df_minvar = df_minvar.fillna(0)
df_ptf = df_ptf.fillna(0)

# Set risk-free rate
risk_free_rate = 0.01

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[i], return_series[j], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

# Considering portfolios with Commodities and transaction costs. (cmds_ntc)
# Load data (Note: Replace with your local paths)
df_a2c = pd.read_csv('/content/drive/MyDrive/cmds_ntc/df_daily_return_a2c.csv', index_col=0)
df_ddpg = pd.read_csv('/content/drive/MyDrive/cmds_ntc/df_daily_return_ddpg.csv', index_col=0)
df_minvar = pd.read_csv('/content/drive/MyDrive/cmds_ntc/df_daily_return_minvar.csv', index_col=0)
df_ppo = pd.read_csv('/content/drive/MyDrive/cmds_ntc/df_daily_return_ppo.csv', index_col=0)
df_ptf = pd.read_csv('/content/drive/MyDrive/cmds_ntc/df_daily_return_ptf.csv', index_col=0)
df_sac = pd.read_csv('/content/drive/MyDrive/cmds_ntc/df_daily_return_sac.csv', index_col=0)
df_td3 = pd.read_csv('/content/drive/MyDrive/cmds_ntc/df_daily_return_td3.csv', index_col=0)

# Fill NaN and ensure date format
df_a2c["date"] = pd.to_datetime(df_a2c["date"])
df_ddpg["date"] = pd.to_datetime(df_ddpg["date"])
#df_minvar["date"] = pd.to_datetime(df_minvar["date"])
df_ppo["date"] = pd.to_datetime(df_ppo["date"])
#df_ptf["date"] = pd.to_datetime(df_ptf["date"])
df_sac["date"] = pd.to_datetime(df_sac["date"])
df_td3["date"] = pd.to_datetime(df_td3["date"])

df_minvar = df_minvar.fillna(0)
df_ptf = df_ptf.fillna(0)

# Set risk-free rate
risk_free_rate = 0.01

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[i], return_series[j], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

#####
# Comparing the results of the portfolios proposed by the models with the Ledoit-Wolf test statistic
#####
import pandas as pd
import numpy as np
from scipy.stats import norm

################################################################################
# Define function for Ledoit-Wolf test statistic
def ledoit_wolf_test(r1, r2, risk_free_rate):
    mean_r1 = r1.mean()
    mean_r2 = r2.mean()
    std_r1 = r1.std()
    std_r2 = r2.std()

    SR1 = (mean_r1 - risk_free_rate) / std_r1
    SR2 = (mean_r2 - risk_free_rate) / std_r2


    cov_matrix = np.cov(r1, r2)
    var_diff = (1 + 2 * (SR1 - SR2) * cov_matrix[0, 1] + (SR1 - SR2) * 2 * (cov_matrix[0, 0] / (std_r1 * 2) + cov_matrix[1, 1] / (std_r2 ** 2))) / (r1.shape[0] - 1)

    Z = (SR1 - SR2) / np.sqrt(abs(var_diff))
    p_value = 2 * (1 - norm.cdf(np.abs(Z)))

    return Z, p_value

################################################################################
# Considering portfolios without Commodities and
# without transaction costs. (stocks_tc)

# Load data (Note: Replace with your local paths)

filename='ret_no_transactions_cost_stocks'
return_series = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_series.drop('id', axis='columns', inplace=True)
# Set risk-free rate
risk_free_rate = 0.01

strategy_names = ["A2C", "DDPG", "MINVAR", "PPO", "PTF", "SAC","TD3"]

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[strategy_names[i]], return_series[strategy_names[j]], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

#Save as Excel file
df_result_test= pd.DataFrame(data=LW_results)
df_result_test.to_excel('test_'+filename+'.xlsx')

################################################################################################
# Considering portfolios with Commodities and
# without transaction costs. (stocks_tc)

# Load data (Note: Replace with your local paths)
filename='ret_No_Transactions_cost_stocks_cmds'
return_series = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_series.drop('id', axis='columns', inplace=True)
# Set risk-free rate
risk_free_rate = 0.01

strategy_names = ["A2C", "DDPG", "MINVAR", "PPO", "PTF", "SAC","TD3"]

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[strategy_names[i]], return_series[strategy_names[j]], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

df_result_test= pd.DataFrame(data=LW_results)
df_result_test.to_excel('test_'+filename+'.xlsx')

################################################################################################
# Considering portfolios without Commodities and
# with transaction costs. (stocks_tc)

# Load data (Note: Replace with your local paths)
filename='ret_ptf_opt_stocks'
return_series = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_series.drop('id', axis='columns', inplace=True)
# Set risk-free rate
risk_free_rate = 0.01

strategy_names = ["A2C", "DDPG", "MINVAR", "PPO", "PTF", "SAC","TD3"]

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[strategy_names[i]], return_series[strategy_names[j]], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

df_result_test= pd.DataFrame(data=LW_results)
df_result_test.to_excel('test_'+filename+'.xlsx')

################################################################################################
# Considering portfolios with Commodities and
# with transaction costs. (stocks_tc)

# Load data (Note: Replace with your local paths)
filename='ret_ptf_opt_stocks_cmds'
return_series = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_series.drop('id', axis='columns', inplace=True)
# Set risk-free rate
risk_free_rate = 0.01

strategy_names = ["A2C", "DDPG", "MINVAR", "PPO", "PTF", "SAC","TD3"]

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    for j, str2 in enumerate(strategy_names):
        if i < j:
            Z, p_value = ledoit_wolf_test(return_series[strategy_names[i]], return_series[strategy_names[j]], risk_free_rate)
            LW_results[(str1, str2)] = {"Z": Z, "p_value": p_value}

# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

df_result_test= pd.DataFrame(data=LW_results)
df_result_test.to_excel('test_'+filename+'.xlsx')

#####
#Add analysis among strategies
# (only stock & no tc, only stock & WITH tc,
# including cmds & no tc, including cmds & WITH tc)
#####
filename='ret_no_transactions_cost_stocks'
return_ntc_ncmd = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_ntc_ncmd.drop('id', axis='columns', inplace=True)

filename='ret_No_Transactions_cost_stocks_cmds'
return_ntc_cmd = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_ntc_cmd.drop('id', axis='columns', inplace=True)

filename='ret_ptf_opt_stocks'
return_tc_ncmd = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_tc_ncmd.drop('id', axis='columns', inplace=True)

filename='ret_ptf_opt_stocks_cmds'
return_tc_cmd = pd.read_csv('/content/stocks_tc/'+filename+'.csv')
return_tc_cmd.drop('id', axis='columns', inplace=True)

# Set risk-free rate
risk_free_rate = 0.01

strategy_names = ["A2C", "DDPG", "MINVAR", "PPO", "PTF", "SAC","TD3"]

# Calculate the LW test statistic for all pairs of strategies
LW_results = {}
for i, str1 in enumerate(strategy_names):
    Z, p_value = ledoit_wolf_test(return_ntc_ncmd[strategy_names[i]], return_ntc_cmd[strategy_names[i]], risk_free_rate)
    LW_results[(str1, 'ntc_ncmd_x_ntc_cmd')] = {"Z": Z, "p_value": p_value}

    Z, p_value = ledoit_wolf_test(return_ntc_ncmd[strategy_names[i]], return_tc_ncmd[strategy_names[i]], risk_free_rate)
    LW_results[(str1, 'ntc_ncmd_x_tc_ncmd')] = {"Z": Z, "p_value": p_value}

    Z, p_value = ledoit_wolf_test(return_ntc_ncmd[strategy_names[i]], return_tc_cmd[strategy_names[i]], risk_free_rate)
    LW_results[(str1, 'ntc_ncmd_x_tc_cmd')] = {"Z": Z, "p_value": p_value}

    Z, p_value = ledoit_wolf_test(return_ntc_cmd[strategy_names[i]], return_tc_ncmd[strategy_names[i]], risk_free_rate)
    LW_results[(str1, 'ntc_ncmd_x_ntc_cmd')] = {"Z": Z, "p_value": p_value}

    Z, p_value = ledoit_wolf_test(return_ntc_cmd[strategy_names[i]], return_tc_cmd[strategy_names[i]], risk_free_rate)
    LW_results[(str1, 'ntc_ncmd_x_tc_cmd')] = {"Z": Z, "p_value": p_value}

    Z, p_value = ledoit_wolf_test(return_tc_ncmd[strategy_names[i]], return_tc_cmd[strategy_names[i]], risk_free_rate)
    LW_results[(str1, 'ntc_ncmd_x_tc_cmd')] = {"Z": Z, "p_value": p_value}


# Display the results
print("Ledoit-Wolf Test Results:")
for pair, result in LW_results.items():
    print(f"{pair[0]} vs. {pair[1]}: Z = {result['Z']:.3f}, p-value = {result['p_value']:.3f}")

df_result_test= pd.DataFrame(data=LW_results)
df_result_test.to_excel('test_strategy_models.xlsx')

return_series.head()

